import requests
import re
import os 
import sys
import time 
ROOT_PATH = os.getcwd()
TEST_DIR = os.path.join(ROOT_PATH, 'test')
AUDIO_DIR = os.path.join(ROOT_PATH, 'audio files')
for dir in [TEST_DIR, AUDIO_DIR]:
    try:
        os.mkdir(dir)
    except:
        print('[Warning]"{}" already exists! (You can ignore this warning)'.format(dir))

base_url = 'http://download.dogwood.com.cn/online/6jchlx/'
amount = 32
audio_url_list = [ base_url+'%02d'%i+'.mp3'  for i in range(1,33)]

response = requests.get(audio_url_list[2])
byte_chunk_list = response.iter_content(chunk_size=2048)
byte_chunk_list = list(byte_chunk_list)
total_chunk_amount = len(byte_chunk_list)

start = time.time()
with open('./test/05-test1.mp3', 'wb') as fp:
    print('Writing data...')
    count = 0
    for chunk in byte_chunk_list:
        count+=1
        os.system('cls')
        print('Writing data {:.2f}\%...'.format(count/total_chunk_amount * 100))
        fp.write(chunk)
end = time.time()
print('Done!Using {:.02f} s'.format(end - start))
